﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Web;
using Top.Web.Api;
using Top.Core;
using Top.Data;
using Top.PointStore.Business.Models;

namespace Top.PointStore.Business
{
    public class PointStoreAuthManager
    {
        public static async Task<int> Auth_AddUser(T_PointStoreAdminAuthority model)
        {
            T_PointStoreAdminAuthority lgobj = await PointStoreAuthManager.Auth_GetAuthByLoginName(model.F_LoginName);
            if (lgobj!=null)
            {
                if (lgobj.F_LoginName == model.F_LoginName)
                {
                    throw new WebApiException("该用户已存在");
                }
            }           
            string sql = @"
            insert into T_PointStoreAdminAuthority(F_AuthorityID,F_CityKid,F_CityName,F_LoginName,F_IsOpen)
            values ({0:F_AuthorityID},{0:F_CityKid},{0:F_CityName},{0:F_LoginName},{0:F_IsOpen})
            ";            
            using (var db = new DbTour())
            {
                return await db.Sql(sql,model).ExecuteNonQuery();
            }
        }

        public static async Task<int> Auth_EditUser(T_PointStoreAdminAuthority model)
        {
            string sql = @"
            update T_PointStoreAdminAuthority
            set F_AuthorityID={0:F_AuthorityID},
                F_CityKid={0:F_CityKid},
                F_CityName = {0:F_CityName},
                F_IsOpen = {0:F_IsOpen}
            where F_LoginName={0:F_LoginName}
            ";
            using (var db = new DbTour())
            {
                return await db.Sql(sql,model).ExecuteNonQuery();
            }
        }

        public static async Task<IList<T_PointStoreAdminAuthority>> Auth_GetList(Pager pager,int cityKid,int isOpen,string loginName,T_PointStoreAdminAuthority auth)
        {
            using (var db = new DbTour())
            {
                var query = db.Table(@"select * from T_PointStoreAdminAuthority");
                if (auth.F_CityKid!=0)
                {
                    cityKid = auth.F_CityKid;
                }
                if (cityKid>-1)
                {
                    query.And(" F_CityKid={0} ", cityKid);
                }
                if (isOpen>-1)
                {
                    query.And(" F_IsOpen={0} ", isOpen);
                }
                if (!string.IsNullOrWhiteSpace(loginName))
                {
                    query.And(" F_LoginName like {0}",loginName+"%");
                }
                return await query.ToList<T_PointStoreAdminAuthority>(pager.StartNumber, pager.EndNumber, " F_UpdateTime desc");
            }
        }

        public static async Task<IList<AdminCityExpand>> Auth_GetAuthCityList(T_PointStoreAdminAuthority auth)
        {
            using (var db = new DbTour("sqldefault"))
            {
                if (auth.F_AuthorityID == 1)
                {
                    IList<AdminCityExpand> adminCityList = await db.Sql("select F_CityKid,F_Title as F_CityName from S_Framework where F_Type='city'").ToList<AdminCityExpand>();                    
                    adminCityList.Insert(0, new AdminCityExpand() { F_CityKid = 0, F_CityName = "全国" });
                    adminCityList.Insert(0, new AdminCityExpand() { F_CityKid = -1, F_CityName = "请选择" });
                    return adminCityList;
                }
                else
                {
                    IList<AdminCityExpand> list = new List<AdminCityExpand>();
                    list.Add(new AdminCityExpand()
                    {
                        F_CityKid = auth.F_CityKid,
                        F_CityName = auth.F_CityName
                    });
                    return list;
                }
            }
        }

        public static async Task<T_PointStoreAdminAuthority>Auth_GetAuthByLoginName(string loginName)
        {
                using (var db = new DbTour())
                {
                    if (string.IsNullOrWhiteSpace(loginName))
                    {
                        return null;
                    }
                    string sql = @"select * from T_PointStoreAdminAuthority where F_LoginName={0} limit 1";

                    return await db.Sql(sql, loginName.Trim()).FirstOrDefault<T_PointStoreAdminAuthority>();
                }
        }       
    }
}
